package fictional.GUI;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import fictional.Common.ShortCharacterItem;

/**
 * A popup which is shown when a user wishes to send a private
 * messages to another user.
 */
@SuppressWarnings("serial")
public class PrivateMessagePopup extends JFrame
{
	ShortCharacterItem friend;
	GUI gui;
	JTextArea messageTextField;
	
	public PrivateMessagePopup(ShortCharacterItem friend, GUI gui)
	{
		this.friend = friend;
		this.gui = gui;
        gui.setEnabled(false);
        gui.setIsEnabled(false);
		
        initFrame();
	}
	
	private void initFrame()
	{
		
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setSize(350, 350);
        this.setLocationRelativeTo(null);
        this.setBackground(Color.white);
        this.setTitle("Send Private Message");
		this.setIconImage(new ImageIcon(getClass().getResource("resources/windowIcon.gif")).getImage());
        
        this.addWindowListener(new WindowAdapter() {
        	public void windowClosing(java.awt.event.WindowEvent e) {
                gui.setIsEnabled(true);
                gui.setEnabled(true);
        	}
        });
        
        GridBagLayout gridBagLayout = new GridBagLayout();
        this.setLayout(gridBagLayout);
        
        GridBagConstraints cons = new GridBagConstraints();
        
        JLabel titleLabel = new JLabel("Send a private message to " + friend.getName() + ":");
        titleLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
        cons.gridx = 0;
        cons.gridy = 0;
        cons.gridwidth = 2;
        cons.insets = new Insets(10,10,10,10);
        cons.anchor = GridBagConstraints.WEST;
        
        this.add(titleLabel, cons);
        
        messageTextField = new JTextArea();
        messageTextField.setBackground(new java.awt.Color(244, 242, 242));
        messageTextField.setFont(new java.awt.Font("Tahoma", 0, 13));
        messageTextField.setColumns(20);
        messageTextField.setRows(4);
        messageTextField.setLineWrap(true);
        messageTextField.setWrapStyleWord(true);
        messageTextField.setAutoscrolls(false);
        messageTextField.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(223, 212, 212)));
        cons.gridx = 0;
        cons.gridy = 1;
        cons.gridwidth = 2;
        cons.weighty = 1.0;
        cons.weightx = 1.0;
        cons.insets = new Insets(0,10,10,10);
        cons.fill = GridBagConstraints.BOTH;
        
        this.add(messageTextField, cons);
        
        JButton OKButton = new JButton("OK");
        cons.gridx = 0;
        cons.gridy = 2;
        cons.gridwidth = 1;
        cons.weighty = 0.0;
        cons.weighty = 0.0;
        cons.insets = new Insets(0,10,10,10);
        cons.fill = GridBagConstraints.NONE;
        cons.anchor = GridBagConstraints.CENTER;
        this.add(OKButton, cons);
        
        OKButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                okButtonMouseClicked(evt);
            }
        });
        
        JButton CancelButton = new JButton("Cancel");
        cons.gridx = 1;
        cons.gridy = 2;
        this.add(CancelButton, cons);
        
        CancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelButtonMouseClicked(evt);
            }
        });
        
	}
	
	private void okButtonMouseClicked(java.awt.event.MouseEvent evt)
	{
		if (messageTextField.getText().isEmpty())
		{
			return;
		}
		gui.sendPrivateMessage(messageTextField.getText(), friend);
        gui.setEnabled(true);
        gui.setIsEnabled(true);
        this.setVisible(false);
        this.dispose();
	}
	
	private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt)
	{
        gui.setEnabled(true);
        gui.setIsEnabled(true);
        this.setVisible(false);
        this.dispose();
	}
}
